package jhunter;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;

import schema.AppsConfigLoader;
import schema.ext.ActionExt;
import schema.ext.AppExt;
import schema.ext.AppsExt;
import schema.ext.ModuleExt;
import schema.ext.PathInfo;

public class DefaultJHunterContainer implements JHunterContainer {

	private AppsConfigLoader appsConfigLoader;

	@Override
	public ActionExt matchActionUri(PathInfo pathInfo) {
		String path = pathInfo.getPath();
		if (path.startsWith("/")) {
			path = path.substring(1);
		}
		int appIndex = path.indexOf("/");
		if (appIndex > -1) {
			AppsExt appsExt = appsConfigLoader.getAppsExt();
			String appContext = path.substring(0, appIndex);
			if (appsExt.getChildren().containsKey(appContext)) {
				AppExt appExt = (AppExt) appsExt.getChildren().get(appContext);
				path = path.substring(appIndex + 1);
				int moduleIndex = path.indexOf("/");
				String moduleContext = path.substring(0, moduleIndex);
				if (appExt.getChildren().containsKey(moduleContext)) {
					ModuleExt moduleExt = (ModuleExt) appExt.getChildren().get(moduleContext);
					if (moduleExt.isNeedReload()) {
						moduleExt.reload();
					}
					path = path.substring(moduleIndex);
					ActionExt actionExt = moduleExt.getActionMap().get(new PathInfo(path, pathInfo.getMethod()));
					return actionExt;
				}
			}
		}
		return null;
	}

	public AppsConfigLoader getAppsConfigLoader() {
		return appsConfigLoader;
	}

	public void setAppsConfigLoader(AppsConfigLoader appsConfigLoader) {
		this.appsConfigLoader = appsConfigLoader;
	}

	@Override
	public void renderStaticReousrce(String url, String basePath, HttpServletResponse resp) throws IOException {
		String realURL = url.substring(basePath.length());
		if (realURL.startsWith("/")) {
			realURL = realURL.substring(1);
		}
		int appIndex = realURL.indexOf("/");
		if (appIndex > -1) {
			AppsExt appsExt = appsConfigLoader.getAppsExt();
			String appContext = realURL.substring(0, appIndex);
			if (appsExt.getChildren().containsKey(appContext)) {
				AppExt appExt = (AppExt) appsExt.getChildren().get(appContext);
				realURL = realURL.substring(appIndex);
				realURL = FilenameUtils.normalize(realURL, true);

				File file = new File(appExt.getFile()).getParentFile();
				File realFile = new File(file, realURL);

				if(!realFile.getPath().startsWith(file.getPath())) {
					resp.sendError(HttpServletResponse.SC_NOT_FOUND);
					return ;
				}
				if(!realFile.exists()) {
					resp.sendError(HttpServletResponse.SC_NOT_FOUND);
					return ;
				}
				if(realURL.endsWith(".js")) {
					resp.setContentType("application/x-javascript; charset=UTF-8");
				}
				FileInputStream fis = new FileInputStream(realFile);
				
				@SuppressWarnings("resource")
				BufferedInputStream buff = new BufferedInputStream(fis);

				byte[] b = new byte[1024];// 相当于我们的缓存

				long k = 0;// 该值用于计算当前实际下载了多少字节

				// 从response对象中得到输出流,准备下载

				OutputStream myout = resp.getOutputStream();
				// 开始循环下载
				while (k < realFile.length()) {
					int j = buff.read(b, 0, 1024);
					k += j;
					// 将b中的数据写到客户端的内存
					myout.write(b, 0, j);

				}
				myout.flush();
			}
		}else {
			resp.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
	}

}
